package org.xenon.vote.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xenon.vote.listener.OnlineJudgeListener;
import org.xenon.vote.model.pojo.po.Choice;
import org.xenon.vote.model.pojo.po.Judge;
import org.xenon.vote.model.pojo.po.User;
import org.xenon.vote.model.pojo.vo.Board;
import org.xenon.vote.model.pojo.vo.Result;
import org.xenon.vote.service.BallotManager;
import org.xenon.vote.service.CaptchaService;
import org.xenon.vote.service.JudgeService;
import org.xenon.vote.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/judge")
public class JudgeController {
    private final CaptchaService captchaService;
    private final JudgeService judgeService;
    private final UserService userService;
    private final BallotManager manager;

    public JudgeController(CaptchaService captchaService, JudgeService judgeService, UserService userService, BallotManager manager) {
        this.captchaService = captchaService;
        this.judgeService = judgeService;
        this.userService = userService;
        this.manager = manager;
    }

    /**
     * 获取裁判当前投票的状态
     * 所有选项(ID, order, 内容)以及投过票的选项, 总共投票数
     */
    @GetMapping("/status")
    public Result status(HttpServletRequest req) {
        class Res {
            class ChoiceAndId{
                final String choice;
                final int id;

                public ChoiceAndId(String choice, int id) {
                    this.choice = choice;
                    this.id = id;
                }

                public String getChoice() {
                    return choice;
                }

                public int getId() {
                    return id;
                }
            }
            /**
             * 记载数据,,方便前端查询
             */
            final List<ChoiceAndId> choices;
            final List<Integer> status;
            final int limit;

            public Res(Choice[] choices, List<Integer> status, int limit) {
                this.choices = new ArrayList<>();
                for (Choice c : choices)
                    this.choices.add(new ChoiceAndId(c.getDetail(), c.getId()));
                this.status = status;
                this.limit = limit;
            }

            public List<ChoiceAndId> getChoices() {
                return choices;
            }

            public List<Integer> getStatus() {
                return status;
            }
            public int getLimit() {
                return limit;
            }
        }
        String name = (String) req.getSession().getAttribute("user");
        String owner = (String) req.getSession().getAttribute("owner");
        User user = userService.getUSer(owner);
        Board board = manager.getBoard(user.getMail());
        if (board == null)  return new Result(false, "notStart");
        manager.judgeLogin(judgeService.get(name, owner));
        Choice[] choices = new ArrayList<>(board.getChoices().keySet()).toArray(new Choice[]{});
        Arrays.sort(choices);
        List<Integer> status = manager.getStatus(name, owner);
        int limit = status.remove(status.size() - 1);
        return new Result(true, new Res(choices, status, limit));
    }

    /**
     * 给某个选项投票
     *
     * @param id 选项ID
     */
    @PostMapping("/vote")
    public Result vote(int id, HttpServletRequest req) {
        String name = (String) req.getSession().getAttribute("user");
        String owner = (String) req.getSession().getAttribute("owner");
        manager.vote(name, id, owner);
        return new Result(true, id);
    }

    /**
     * 裁判登录
     *
     * @param name     裁判名
     * @param passwd 密码
     * @param captcha  验证码
     */
    @PostMapping("/login")
    public Result login(String owner, String name, String passwd, String captcha, HttpServletRequest req) {
        if (!captchaService.captchaCheck(req, captcha))
            return new Result(false, "captcha");
        Judge get = judgeService.login(name, owner);
        if (get == null)
            return new Result( false, "nameOrPasswd");
        if (!get.getPassword().equals(passwd))
            return new Result( false, "nameOrPasswd");
        manager.judgeLogin(get);
        HttpSession session = req.getSession();
        session.setMaxInactiveInterval(10);
        session.setAttribute("user", name);
        session.setAttribute("owner", get.getOwner());
        session.setAttribute("group", "judge");
        OnlineJudgeListener.login(get, session.getId());
        return new Result(true, name);
    }
}
